package tests.datastructures.stacks;

import static org.junit.Assert.*;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import datastructures.stacks.StackADT;
import datastructures.stacks.LinkedStackWithSentinelNode;

public class LinkedStackTestCase {

	private StackADT<String> stack;

	@Before
	public void setUp() throws Exception {
		this.stack = new LinkedStackWithSentinelNode<String>();
	}

	@After
	public void tearDown() throws Exception {
		this.stack = null;
	}

	@Test
	public void testInitialInstance() {
		assertTrue(this.stack.isEmpty());
		assertEquals(0, this.stack.size());
	}

	@Test
	public void testRetrievingDataFromEmtpyStack() {
		assertEquals(null, this.stack.peek());
		assertEquals(null, this.stack.pop());
	}

	@Test
	public void testPopAndPeekMethods() {
		String testValue1 = "Gatsby";
		String testValue2 = "is";
		String testValue3 = "the";
		String testValue4 = "best";
		this.stack.push(testValue1);
		assertEquals(testValue1, this.stack.peek());
		this.stack.push(testValue2);
		assertEquals(testValue2, this.stack.peek());
		this.stack.push(testValue3);
		assertEquals(testValue3, this.stack.peek());
		this.stack.push(testValue4);
		assertEquals(testValue4, this.stack.peek());
		assertFalse(this.stack.isEmpty());
		assertEquals(4, this.stack.size());
		assertEquals(testValue4, this.stack.peek());
		assertEquals(testValue4, this.stack.pop());
		assertEquals(testValue3, this.stack.pop());
		assertEquals(testValue2, this.stack.pop());
		assertEquals(testValue1, this.stack.pop());
	}

	@Test(expected=NoSuchElementException.class)
	public void testIteratorWithNoSuchElementException() {
		Iterator<String> iterator = this.stack.iterator();
		iterator.next();
	}

	@Test(expected=UnsupportedOperationException.class)
	public void testRemoveInIteratorWithUnsupportedOperationException() {
		Iterator<String> iterator = this.stack.iterator();
		iterator.remove();
	}
}
